package com.zhouj.endless.geohash.util;


import com.zhouj.endless.geohash.BoundingBox;
import com.zhouj.endless.geohash.GeoHash;

/**
 * Created by IntelliJ IDEA. User: kevin Date: Jan 17, 2011 Time: 12:03:47 PM
 *  多边形外界框(由两个GeoHash形成的边界框)
 */
public class TwoGeoHashBoundingBox {
    /**
     * 地理围栏
     */
    private BoundingBox boundingBox;
    /**
     * 西南角
     */
    private GeoHash southWestCorner;
    /**
     * 东北角
     */
    private GeoHash northEastCorner;

    /**
     *
     * @param bbox  多边形外界框
     * @param numberOfCharacters 期望精度
     * @return
     */
    public static TwoGeoHashBoundingBox withCharacterPrecision(BoundingBox bbox, int numberOfCharacters) {
        /**
         * 此方法使用给定数量的字符作为所需的精度值。散列只能是64位长，因此可以实现12个字符的最大精度。
         */
        //      西南角                                            最小纬度(南纬)                最小经度(西经)            期望精度
        GeoHash southWestCorner = GeoHash.withCharacterPrecision(bbox.getSouthLatitude(), bbox.getWestLongitude(), numberOfCharacters);
        //      东北角                                           最大纬度(北纬)                最大经度(东经)            期望精度
        GeoHash northEastCorner = GeoHash.withCharacterPrecision(bbox.getNorthLatitude(), bbox.getEastLongitude(), numberOfCharacters);
        //GeoHash 该类主要提供了按照指定的geohash长度(字符串长度或者bit位长度)，将经纬度转为对应的GeoHash对象，以及GeoHash相关的操作
        return new TwoGeoHashBoundingBox(southWestCorner, northEastCorner);
    }

    public static TwoGeoHashBoundingBox withBitPrecision(BoundingBox bbox, int numberOfBits) {
        GeoHash southWestCorner = GeoHash.withBitPrecision(bbox.getSouthLatitude(), bbox.getWestLongitude(), numberOfBits);
        GeoHash northEastCorner = GeoHash.withBitPrecision(bbox.getNorthLatitude(), bbox.getEastLongitude(), numberOfBits);
        return new TwoGeoHashBoundingBox(southWestCorner, northEastCorner);
    }

    public TwoGeoHashBoundingBox(GeoHash southWestCorner, GeoHash northEastCorner) {
        if (southWestCorner.significantBits() != northEastCorner.significantBits()) {
            throw new IllegalArgumentException("在具有不同精度的哈希之间迭代有意义吗？");
        }
        this.southWestCorner = GeoHash.fromLongValue(southWestCorner.longValue(), southWestCorner.significantBits());//西南角
        this.northEastCorner = GeoHash.fromLongValue(northEastCorner.longValue(), northEastCorner.significantBits());//东北角

        //地理围栏
        boundingBox = new BoundingBox(  southWestCorner.getBoundingBox().getSouthLatitude(), //西南角.地理围栏.最小纬度(南纬)
                                        northEastCorner.getBoundingBox().getNorthLatitude(), //东北角.地理围栏.最大纬度(北纬)
                                        southWestCorner.getBoundingBox().getWestLongitude(), //西南角.地理围栏.最小经度(西经)
                                        northEastCorner.getBoundingBox().getEastLongitude()  //东北角.地理围栏.最大经度(东经)
        );
    }

    public BoundingBox getBoundingBox() {
        return boundingBox;
    }

    public GeoHash getSouthWestCorner() {
        return southWestCorner;
    }

    public GeoHash getNorthEastCorner() {
        return northEastCorner;
    }

    public String toBase32() {
        return southWestCorner.toBase32() + northEastCorner.toBase32();
    }
}
